package com.skiving.cloud;

import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.EntityConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.dialect.IDialect;
import com.mybatisflex.codegen.dialect.JdbcTypeMapping;
import com.mybatisflex.codegen.generator.GeneratorFactory;
import com.zaxxer.hikari.HikariDataSource;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class MybatisFlexCodegen {
    //数据库
    public static final String JDBC_URL = "jdbc:mysql://localhost:3306/skivingcloud_job?useUnicode=true&characterEncoding=utf8&useSSL=false";
    //数据库用户
    public static final String JDBC_USERNAME = "root";
    //数据库密码
    public static final String JDBC_PASSWORD = "nzmn!1C1C$y7g4s8w";
    //代码注释中的作者
    public static final String AUTHOR = "skivingcloud";
    //指定输出目录
    public static final String SOURCE_DIR = "D:\\\\javaprogram\\\\mybatisflexgenerator";
    //mybatis-xml目录
    public static final String MAPPER_XML_PATH = "D:\\\\javaprogram\\\\mybatisflexgenerator\\\\com\\\\skiving\\\\cloud\\\\job\\\\mapping";
    //设置根包
    public static final String BASE_PACKAGE = "com.skiving.cloud.job";
    //表前缀，生成代码时会去掉前缀
    public static final String TABLE_PREFIX = "tb_";
    //要生成代码的表，多个逗号隔开
    public static final Set<String> GENERATE_TABLE = new HashSet<>(Arrays.asList(
            "xxl_job_info",
            "xxl_job_log",
            "xxl_job_log_report",
            "xxl_job_logglue",
            "xxl_job_registry",
            "xxl_job_group",
            "xxl_job_user",
            "xxl_job_lock"
    ));

    public static void main(String[] args) {
        //配置数据源
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl(JDBC_URL);
        dataSource.setUsername(JDBC_USERNAME);
        dataSource.setPassword(JDBC_PASSWORD);
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");

        GlobalConfig globalConfig = createGlobalConfigUseStyle();
        //通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig, IDialect.MYSQL);
        //使用LocalDateTime作为日期类型
        JdbcTypeMapping.registerMapping(Timestamp.class, LocalDateTime.class);
        //前端页面生成模板Vue3+element-plus+ts
        GeneratorFactory.registerGenerator("html",new VueElementTsGenerator());
        GeneratorFactory.registerGenerator("apiTs",new VueApiTsGenerator());
        //生成代码
        generator.generate();
    }

    public static GlobalConfig createGlobalConfigUseStyle() {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setAuthor(AUTHOR);
        globalConfig.setEntityJdkVersion(17);
        // 指定输出目录
        globalConfig.setSourceDir(SOURCE_DIR);
        globalConfig.setMapperXmlPath(MAPPER_XML_PATH);
        //设置根包
        globalConfig.getPackageConfig()
                .setBasePackage(BASE_PACKAGE);
        //设置表前缀和只生成哪些表，setGenerateTable 未配置时，生成所有表
        globalConfig.getStrategyConfig()
                .setTablePrefix(TABLE_PREFIX)
                .setGenerateTables(GENERATE_TABLE)
                .setGenerateForView(true);
        //模板设置
        globalConfig.getTemplateConfig()
                .setController("./templates/controller-Solon.tpl")
                .setService("./templates/service.tpl")
                .setServiceImpl("./templates/serviceImpl-Solon.tpl");
        //设置生成 entity
        globalConfig.enableEntity()
                .setOverwriteEnable(true)
                .setWithSwagger(true)
                .setSwaggerVersion(EntityConfig.SwaggerVersion.FOX)
                .setWithActiveRecord(false)
                .setWithLombok(true);
        //设置生成 mapperXml
        globalConfig.enableMapperXml().setOverwriteEnable(true);
        //设置生成 mapper
        globalConfig.enableMapper().setOverwriteEnable(true);
        //设置生成 service
        globalConfig.enableService().setOverwriteEnable(true);
        globalConfig.enableServiceImpl().setOverwriteEnable(true);
        //设置生成 controller
        globalConfig.enableController().setOverwriteEnable(true);

        return globalConfig;
    }

}
